package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/12 11:03
 * @description
 */
public class Code530 {
    //private int min = 0;
    //
    //public int getMinimumDifference(TreeNode root) {
    //    getMin(root, 999);
    //    return min;
    //}
    //
    //private void getMin(TreeNode node, int val) {
    //    if (node != null) {
    //        min = Math.min(min, Math.abs(node.val - val));
    //        getMin(node.left, node.val);
    //        getMin(node.right, node.val);
    //    }
    //}

    private List<Integer> result = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {
        int min = 999;
        setList(root);
        Collections.sort(result);
        for (int i = 1; i < result.size(); i++) {
            min = Math.min(min, result.get(i) - result.get(i - 1));
        }
        return min;
    }

    private void setList(TreeNode node) {
        if (node != null) {
            result.add(node.val);
            setList(node.left);
            setList(node.right);
        }
    }
}
